const network = '';
const baseurl = network + '/api';



const request = (obj, type, api) => {
	const token = uni.getStorageSync('token');
	if (token) {
		if (!obj.data) {
			obj.data = {}
		}
		obj.data.staff_token = uni.getStorageSync('token');
	}
	return new Promise(function(reslove, reject) {
		uni.request({
			url: baseurl + obj.url,

			data: obj.data,
			method: type,
			header: obj.header ? obj.header : {
				"Content-Type": "application/json"
			},

			success(res) {
				
				
				if (res.data.code === 1) {
					reslove(res.data);
				} else {
					if(res.data.msg ==="token失效"){
						uni.showToast({
							icon: 'none',
							title: '登录过期',
						});
						setTimeout(()=>{
							uni.reLaunch({
								url:"/pages/index/index"
							})
						},1500)
						
						return
					}
					if(res.data.msg ==="该合同已经签约过了不再重复签约"){
						reject(err);
						return
					}
					if (res.data.msg === "证书已经申请过了") {
						reslove(res.data);
						return
					}
					uni.showToast({
						icon: 'none',
						title: res.data.msg
					});
				}
				// uni.navigateTo({
				// 	url: "/pages/login/login",
				// })

			},

			fail(err) {
				console.log(err)
				uni.showToast({
					icon: 'none',
					title: '请求超时，请稍后再试'
				});
				reject(err);
			}
		});
	});
};

const get = obj => {
	return request(obj, 'GET');
};


const post = obj => {
	return request(obj, 'POST');
};



const __uploadFile = (obj, type) => {
	const token = uni.getStorageSync('token');
	if (token) {
		if (!obj.data) {
			obj.data = {}
		}
		obj.data.staff_token = uni.getStorageSync('token');
	}
	return new Promise((reslove, reject) => {

		const upload = (obj, tempFilePaths, type) => {

			uni.uploadFile({
				url: baseurl + obj.url,
				filePath: tempFilePaths,
				
				sourceType: obj.data.sourceType ? obj.data.sourceType : ["camera",
					"album"
				],
				name: 'image',
				success: (uploadFileRes) => {
					reslove({
						preview: tempFilePaths,
						src: JSON.parse(uploadFileRes.data).data,
					});
				},
				fail: (e) => {

					reject(e)
				}
			});

		}
		const upload1 = (obj, tempFilePaths, type) => {
		
			uni.uploadFile({
				url: baseurl + obj.url,
				
				file: tempFilePaths,
				sourceType: obj.data.sourceType ? obj.data.sourceType : ["camera",
					"album"
				],
				name: 'image',
				success: (uploadFileRes) => {
					reslove({
						preview: JSON.parse(uploadFileRes.data).data,
						src: JSON.parse(uploadFileRes.data).data,
					});
				},
				fail: (e) => {
					reject(e)
				}
			});
		
		}
		uni.chooseImage({
			count: 1,
			sourceType: obj.data.sourceType ? obj.data.sourceType : ["camera", "album"],
			// crop: {
			// 	quality:obj.data.wCompress?"1":"80",
			// 	width:"300",
			// 	height:"300",
			// 	resize:false
			// },
			success: (chooseImageRes) => {
				const tempFilePaths = chooseImageRes.tempFilePaths[0];
				if (obj.data.wCompress) {
					uni.showLoading({
						title: "正在压缩图片"
					})
					obj.data.wCompress.start(tempFilePaths, {
							pixels: 100000, // 最大分辨率，默认二百万
							quality: 0.1, // 压缩质量，默认0.8
							type: 'png', // 图片类型，默认jpg
							base64: false, // 是否返回base64，默认false，非H5有效
						})
						.then(response => {
							// uni.showToast({
							// 	icon: 'none',
							// 	title: "图片压缩成功"
							// });

							uni.showLoading({title: "正在校验..."})
							// console.log(response, response[0])
							// let fileUrl = ''
							// const d = JSON.parse(response.data)
							// fileUrl = d.data



							upload1(obj, response, type)
						})
						.catch(e => {
							uni.hideLoading()
							console.log(e)

						})
					return
				}
				upload(obj, tempFilePaths, type)


			}
		});

	});
}


module.exports = {
	post,
	get,
	// network,
	__uploadFile,
	// getApi,
};